//
//  LoginVC.m
//  Login
//
//  Created by mac on 17/2/21.
//  Copyright © 2017年 ZF. All rights reserved.
//

#import "LoginVC.h"
#import "NetworkLink.h"
#import "MainView.h"

@interface LoginVC () <UITextFieldDelegate>
{
    AppDelegate *delegate;
}
@end

@implementation LoginVC

- (IBAction)onStart:(id)sender
{
    [self finish];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.name endEditing:YES];
    [self.password endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    delegate = [Helps app];
    self.name.clearButtonMode =  UITextFieldViewModeAlways;
    self.name.delegate = self;
    self.name.tag = 8001;
    self.password.clearButtonMode = UITextFieldViewModeAlways;
    self.password.delegate = self;
    self.password.tag = 8002;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(textField.tag == 8001)
    {
        [self.password becomeFirstResponder];
    }
    else if(textField.tag == 8002)
    {
        [self finish];
    }
    return YES;
}

- (void)finish
{
    if([self.name.text  isEqual:@""] || [self.password.text  isEqual:@""]) {
        NSLog(@"请输入");
        [Helps showTips:@"请输入帐号或密码" forTime:0.8 inView:self.view yOffset:1];
        
    }else{
        MBProgressHUD *hud = [Helps showWaitHUDTips:nil yOffset:1];
        [NetworkLink loginWithName:self.name.text Password:self.password.text complete:^(BOOL ok) {
            [hud hide:YES];
            if(ok == YES)
            {
                NSLog(@"正确");
                delegate.ifLogin = YES;
                delegate.userName = self.name.text;
                delegate.password = self.password.text;
                [self getUserDepartment];
                
                [delegate.userdefault setBool:delegate.ifLogin forKey:@"IfLogin"];
                [delegate.userdefault setObject:delegate.userName forKey:@"UserName"];
                [delegate.userdefault setObject:delegate.password forKey:@"Password"];
                [delegate.userdefault synchronize];
                
                [Helps setNaviControllerRootView];
                [Helps getTabCon].selectedIndex = 0;
                
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    }
}

- (void)getUserDepartment
{
    [NetworkLink userLoginListComplete:^(UserList *list){
        delegate.userlist = [NSMutableArray arrayWithArray:list.record.data];
        [delegate.userlist enumerateObjectsUsingBlock:^(UserListData * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString *str = obj.name;
            NSString *str2 = delegate.userName;
           
            if([str.uppercaseString isEqualToString:str2.uppercaseString])
            {
                delegate.userID = [NSString stringWithFormat:@"%ld",obj.Id];
                delegate.department = obj.department;
                NSLog(@"YOU ID NUMBER:%@,部门:%@",delegate.userID,delegate.department);
            }
 
        }];
        [delegate.userdefault setObject:delegate.userID forKey:@"UserID"];
        [delegate.userdefault setObject:delegate.department forKey:@"UserDepart"];
        [delegate.userdefault synchronize];
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
